import java.util.Arrays;

/**
 * Created by forcht on 2018/5/22.
 */
public class Solution {
    public void nextPermutation(int[] num) {
        if (num==null||num.length<2)return;
        int i=num.length-1;
        while (i>0&&num[i]<=num[i-1])i--;
        if (i==0){
           Arrays.sort(num);
        }else {
            int j=num.length-1;
            while (num[j]<=num[i-1])j--;
            int t=num[j];
            num[j]=num[i-1];
            num[i-1]=t;
            Arrays.sort(num,i,num.length);
        }

    }

    public static void main(String[] args) {
        Solution solution=new Solution();
        int[] a={1,3,2};
        solution.nextPermutation(a);
        for(int i=0;i<a.length;i++){
            System.out.print(a[i]+" ");
        }
    }
}